<!DOCTYPE html>

<head>
    <meta charset="utf-8">
    <title>Promise-test1.html</title>
</head>

<body>
    <script>
        // .then中throw出的error可被.catch处理到
        // 当then中两参数都有定义，then(()=>{},()=>{})，而且还有catch的时候，then中的第二个优先；但是如果then中
        // 有throw new Error()抛出异常，则只能被.catch处理到，而不会被.then的第二个参数处理
        var promise = new Promise(function (resolve, reject) {
            console.log('start new Promise...');
            var timeOut = 1;
            console.log('set timeout to: ' + timeOut + ' seconds.');
            setTimeout(function () {
                if (timeOut < 1) {
                    console.log('call resolve()1...');
                    resolve('200 OK,pedding to fulfilled');
                } else {
                    console.log('call reject()...');
                    reject('timeout in ' + timeOut + ' seconds.');
                }
            }, timeOut * 1000);
        }).then(function (r) {
            console.log('Done: ' + r);
            throw new Error('error!!!')
        },function(r){
            console.log('Done2' + r)
            throw new Error('error2!!!')
        }).catch(function (reason) {
            console.log('Failed: ' + reason);
        });
    </script>
</body>

</html>